package com.paipai.helper.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

import static android.content.Context.MODE_PRIVATE;

public class SPConstant {
    public final static String SP_FILE_NAME = "PositionSP";
    public final static String SP_COLLECT_X = "CollectX";//收取种子X
    public final static String SP_COLLECT_Y = "CollectY";//收取种子Y
    public final static String SP_SEED_X = "SeedX";//种植X
    public final static String SP_SEED_Y = "SeedY";//种植Y
    public final static String SP_SEED_COUNT = "SeedCount";//种植数量
    public final static String SP_COUNT_DOWN = "CountDown";//倒计时


    public static void saveDataToCache(Context context, Map<String, Object> position){
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        for(String key : position.keySet()){
            if(position.get(key) instanceof  Integer){
                editor.putInt(key, Integer.parseInt(position.get(key).toString()));
            } else if(position.get(key) instanceof  Long){
                editor.putLong(key, Long.parseLong(position.get(key).toString()));
            } else if(position.get(key) instanceof  String){
                editor.putString(key, position.get(key).toString());
            } else if(position.get(key) instanceof  Boolean){
                editor.putBoolean(key, Boolean.parseBoolean(position.get(key).toString()));
            } else if(position.get(key) instanceof  Float){
                editor.putFloat(key, Float.parseFloat(position.get(key).toString()));
            }

        }
        editor.apply();
    }

    public static int getIntDataFromCacheByKey(Context context, String key){
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
        return sp.getInt(key, 0);
    }

    public static long getLongDataFromCacheByKey(Context context, String key){
        SharedPreferences sp = context.getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
        return sp.getLong(key, 0);
    }
}
